<template>
  <div class='container'>
    <el-card style="margin: 10px 15px">
      <el-row>
        <!-- 表单 -->
        <el-col :span="18">
          <el-form :inline="true" size="small">
            <el-form-item label="学科名称">
              <el-input v-model="reqParms.subjectName"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="clear()">清除</el-button>
            <el-button @click="filter()" type="primary">搜索</el-button>
          </el-form-item>
        </el-form>
        </el-col>

        <!-- 按钮 -->
        <el-col :span="6">
         <el-button style="float:right" icon="el-icon-edit" @click="subjectDialog()" type="success" size="small">新增学科</el-button>
        </el-col>
      </el-row>
      <el-alert :title="`数据一共${total}条`" type="info" :closable="false" style="margin-bottom: 15px" show-icon>
        </el-alert>
        <el-table :data="subjects">
            <el-table-column label="序号" type="index" />
            <el-table-column label="学科名称" prop="subjectName" />
            <el-table-column label="创建者" prop="username" />
            <el-table-column label="创建日期" width="160px">
              <template slot-scope="scope">{{scope.row.addDate|parseTimeByString}}</template>
            </el-table-column>
            <el-table-column label="前台是否显示">
              <template slot-scope="scope">{{scope.row.isFrontDisplay===1?'是':'否'}}</template>
            </el-table-column>
            <el-table-column label="二级目录" prop="twoLevelDirectory" />
            <el-table-column label="标签" prop="tags" />
            <el-table-column label="题目数量" prop="totals" />
            <el-table-column label="操作" width="240px">
              <template slot-scope="scope">
                <el-button type="text" @click="$router.push(`directorys?id=${scope.row.id}&name=${scope.row.subjectName}`)">学科分类</el-button>
                <el-button type="text" @click="$router.push(`tags?id=${scope.row.id}&name=${scope.row.subjectName}`)">学科标签</el-button>
                <el-button type="text" @click="subjectDialog(scope.row)">修改</el-button>
                <el-button type="text" @click="delSubject(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
           <el-pagination
            :current-page="reqParms.page"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="reqParms.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            class="pager"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            >
          </el-pagination>
        <subjects-add ref="subjectadd" :data="currSubject" @handleSubmit="handleSubmit()" />
    </el-card>
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/subjects.js'
import subjectsAdd from '../components/subjects-add.vue'
export default {
  components: { subjectsAdd },
  name: 'subjects-page',
  data () {
    return {
      showDialog: false,
      // 筛选参数
      reqParms: {
        isFrontDisplay: '1',
        subjectName: '',
        page: 1,
        pagesize: 10
      },
      subjects: [],
      total: 0,
      currSubject: {}
    }
  },
  created () {
    this.getSubjects()
  },
  methods: {
    // 删除
    async delSubject (subject) {
      await this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        type: 'warning'
      })
      await remove(subject)
      this.$message.success('删除成功')
      this.getSubjects()
    },
    // 获取表格数据列表
    async getSubjects () {
      const { data } = await list(this.reqParms)
      this.subjects = data.items
      this.total = data.counts
    },
    // 新增 修改
    handleSubmit () {
      if (!this.currSubject.id) {
        this.reqParms.page = 1
      }
      this.getSubjects()
    },
    // 展开对话框
    subjectDialog (subject) {
      this.currSubject = subject || {}
      this.$nextTick(() => {
        this.$refs.subjectadd.open()
      })
    },

    // 清除
    clear () {
      this.reqParms = {
        subjectName: '',
        page: 1,
        pagesize: 10
      }
      this.getSubjects()
    },
    // 搜索
    filter () {
      this.reqParms.page = 1
      this.getSubjects()
    },

    // 切换条数
    handleSizeChange (size) {
      this.reqParms.page = 1
      this.reqParms.pagesize = size
      this.getSubjects()
    },
    // 切换分页
    handleCurrentChange (np) {
      this.reqParms.page = np
      this.getSubjects()
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  .pager {
    margin-top: 15px;
    margin-bottom: 15px;
    float: right;
  }
}
</style>
